﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Resources;

namespace Tuzzit
{
    public partial class RecoveryPassword : PhoneApplicationPage
    {
        public RecoveryPassword()
        {
            InitializeComponent();
            PageTitle.Text = AppResources.PageTitleForgotPwd;
            LabelInfo.Text = AppResources.LabelMail;
            SendMail.Content = AppResources.EnvoyerBouton;
        }

        private void SendEmail(object sender, RoutedEventArgs e)
        {
            //Variables locales
            String mail;
            mail = mailField.Text;

            if (mail.Equals("") || !mail.Contains("@"))
            {
                ResourceManager resourceManager = new ResourceManager("Tuzzit.AppResources", GetType().Assembly);
                MessageBox.Show((String)resourceManager.GetObject("RecoveryPasswordMailEmpty"));
                mailField.BorderBrush = new SolidColorBrush(Colors.Red);
            }
            else 
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://dev.tuzzit.com/exec/recovery_controller.php?function=recovery_password&format=json&params%5Bemail%5D=" + mail);
                request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
            }          
        }

        private void ReadWebRequestCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);

            using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
            {
                string results = httpwebStreamReader.ReadToEnd();
                //MessageBox.Show(results.ToString() //-- on another thread!
                Dispatcher.BeginInvoke(() => mail_success(results.Trim()));
            }
            myResponse.Close();
        }

        private void mail_success(string res)
        {
            //MessageBox.Show(res.ToString());
            if (res.Equals("\"UNKNOWN_USER\""))
            {
                ResourceManager resourceManager = new ResourceManager("Tuzzit.AppResources", GetType().Assembly);
                MessageBox.Show((String)resourceManager.GetObject("RecoveryPasswordNotTuzzer"));
                //MessageBox.Show("You are not a TUZZer. Please create an account !");
            }
            else
            {
                if (res.Equals("false"))
                {
                    ResourceManager resourceManager = new ResourceManager("Tuzzit.AppResources", GetType().Assembly);
                    MessageBox.Show((String)resourceManager.GetObject("RecoveryPasswordErrorConnection"));
                    //MessageBox.Show("An error occured! Your internet connection is probably down!");
                }
                else
                {
                    ResourceManager resourceManager = new ResourceManager("Tuzzit.AppResources", GetType().Assembly);
                    MessageBox.Show((String)resourceManager.GetObject("RecoveryPasswordMailSent"));
                    //MessageBox.Show("An email has been sent to your mailbox!");
                    mailField.Text = "";
                }
            }
        }

        private void RemoveBorderMail(object sender, System.Windows.Input.GestureEventArgs e)
        {
            mailField.BorderBrush = new SolidColorBrush(Colors.Transparent);
        }
    }
}